package top.iflyoung.cim.client.service.impl;

import top.iflyoung.cim.client.client.CIMClient;
import top.iflyoung.cim.client.config.AppConfiguration;
import top.iflyoung.cim.client.service.InnerCommand;
import top.iflyoung.cim.client.service.InnerCommandContext;
import top.iflyoung.cim.client.service.MsgHandle;
import top.iflyoung.cim.client.service.MsgLogger;
import top.iflyoung.cim.client.service.RouteRequest;
import top.iflyoung.cim.client.vo.req.GroupReqVO;
import top.iflyoung.cim.client.vo.req.P2PReqVO;
import top.iflyoung.cim.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Function:
 *
 * @author crossoverJie
 * Date: 2018/12/26 11:15
 * @since JDK 1.8
 */
@Service
public class MsgHandler implements MsgHandle {
    private final static Logger LOGGER = LoggerFactory.getLogger(MsgHandler.class);
    @Autowired
    private RouteRequest routeRequest;

    @Autowired
    private AppConfiguration configuration;

    @Resource(name = "callBackThreadPool")
    private ThreadPoolExecutor executor;

    @Autowired
    private CIMClient cimClient;

    @Autowired
    private MsgLogger msgLogger;

    @Autowired
    private ClientInfo clientInfo;

    @Autowired
    private InnerCommandContext innerCommandContext;

    private boolean aiModel = false;

    @Override
    public void sendMsg(String msg) {
        if (aiModel) {
            aiChat(msg);
        } else {
            normalChat(msg);
        }
    }

    /**
     * 正常聊天
     *
     * @param msg
     */
    private void normalChat(String msg) {

        if (msg.contains(";;")) {
            String[] totalMsg = msg.split(";;");
            if (totalMsg.length > 1) {
                //私聊
                P2PReqVO p2PReqVO = new P2PReqVO();
                p2PReqVO.setUserId(Long.parseLong(configuration.getUserId()));
                p2PReqVO.setReceiveUserId(Long.parseLong(totalMsg[0]));
                p2PReqVO.setMsg(totalMsg[1]);
                try {
                    p2pChat(p2PReqVO);
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            }
        } else if (msg.contains("::")) {
            //分组群聊
            String[] totalMsg = msg.split("::");
            if (totalMsg.length > 1) {
                String groupId = totalMsg[0];
                String message = totalMsg[1];
                GroupReqVO groupReqVO = new GroupReqVO(Long.parseLong(configuration.getUserId()), Long.parseLong(groupId), message);
                try {
                    groupChat(groupReqVO);
                } catch (Exception e) {
                    LOGGER.error("Exception", e);
                }
            }

        }
    }

    /**
     * AI model
     *
     * @param msg
     */
    private void aiChat(String msg) {
        msg = msg.replace("吗", "");
        msg = msg.replace("嘛", "");
        msg = msg.replace("?", "!");
        msg = msg.replace("？", "!");
        msg = msg.replace("你", "我");
        System.out.println("AI:\033[31;4m" + msg + "\033[0m");
    }

    @Override
    public void groupChat(GroupReqVO groupReqVO) throws Exception {
        routeRequest.sendGroupMsg(groupReqVO);
    }

    @Override
    public void p2pChat(P2PReqVO p2PReqVO) throws Exception {
        routeRequest.sendP2PMsg(p2PReqVO);
    }

    @Override
    public boolean checkMsg(String msg) {
        if (StringUtil.isEmpty(msg)) {
            LOGGER.warn("不能发送空消息！");
            return true;
        }
        return false;
    }

    @Override
    public boolean innerCommand(String msg) {

        if (msg.startsWith(":")) {
            InnerCommand instance = innerCommandContext.getInstance(msg);
            instance.process(msg);

            return true;

        } else {
            return false;
        }


    }

    /**
     * 关闭系统
     */
    @Override
    public void shutdown() {
        LOGGER.info("系统关闭中。。。。");
        routeRequest.offLine();
        msgLogger.stop();
        executor.shutdown();
        try {
            while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
                LOGGER.info("线程池关闭中。。。。");
            }
            cimClient.close();
        } catch (InterruptedException e) {
            LOGGER.error("InterruptedException", e);
        }
        System.exit(0);
    }

    @Override
    public void openAIModel() {
        aiModel = true;
    }

    @Override
    public void closeAIModel() {
        aiModel = false;
    }

}
